package translated;

import java.util.List;
import edu.pku.sei.modeler.model.core.*;
import edu.pku.sei.modeler.jet.util.*;

public class ModelRootGen
{
  protected static String nl;
  public static synchronized ModelRootGen create(String lineSeparator)
  {
    nl = lineSeparator;
    ModelRootGen result = new ModelRootGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "/**" + NL + " * Copyright (c) Software Institute of PKU" + NL + " * All rights reserved. " + NL + " * This program and the accompanying materials" + NL + " * are made available under the terms of the Eclipse Public License v1.0" + NL + " * which accompanies this distribution, and is available at" + NL + " * http://www.eclipse.org/legal/epl-v10.html" + NL + " *" + NL + " */" + NL + "package ";
  protected final String TEXT_2 = ";" + NL + "" + NL + "import java.util.ArrayList;" + NL + "import java.util.List;" + NL + "" + NL + "import edu.pku.sei.modeler.model.common.ModelRegistry;" + NL + "import edu.pku.sei.modeler.model.core.MetaDataType;" + NL + "import edu.pku.sei.modeler.model.core.MetaModel;" + NL + "import edu.pku.sei.modeler.model.core.MetaPackage;" + NL + "import edu.pku.sei.modeler.model.core.MetaPrimitiveType;" + NL + "import edu.pku.sei.modeler.model.structure.MetaDiagram;" + NL + "" + NL + "import ";
  protected final String TEXT_3 = ";" + NL + "import ";
  protected final String TEXT_4 = ";" + NL + "" + NL + "/**" + NL + " * @generated" + NL + " *" + NL + " */" + NL + "class ";
  protected final String TEXT_5 = " {" + NL + "\tprotected MetaModel model;" + NL + "\t" + NL + "\tpublic ";
  protected final String TEXT_6 = "() {" + NL + "\t}" + NL + "\t/* (non-Javadoc)" + NL + "\t * @see edu.pku.modeler.internal.IProjectModelRoot#getAllInstances()" + NL + "\t */" + NL + "\tpublic List getAllInstances() {" + NL + "\t\tList list = this.getDiagramInstances();" + NL + "//\t\tlist.add(this.getModelInstances());" + NL + "\t\treturn list;" + NL + "\t}" + NL + "\t" + NL + "\tpublic List getDiagramInstances() {" + NL + "\t\tList list = new ArrayList();" + NL + "//\t\tdiagramRoot.getContentRecursive(list);" + NL + "\t\treturn list;" + NL + "\t}" + NL + "\t/* (non-Javadoc)" + NL + "\t * @see edu.pku.modeler.internal.IProjectModelRoot#getInstancesOf(java.lang.String)" + NL + "\t */" + NL + "\tpublic List getInstancesOf(String typeName) {" + NL + "\t\tList list = getAllInstances();" + NL + "\t\tArrayList result = new ArrayList();" + NL + "\t\tif(typeName.equals(\"Type\"))" + NL + "\t\t{" + NL + "\t\t\tfor(int i=0; i<list.size(); i++) {" + NL + "\t\t\t\tif(list.get(i) instanceof MetaDataType){" + NL + "\t\t\t\t\tresult.add(list.get(i));" + NL + "\t\t\t\t}" + NL + "\t\t\t}" + NL + "\t\t}" + NL + "\t\treturn result;" + NL + "\t}" + NL + "" + NL + "\t/* (non-Javadoc)" + NL + "\t * @see edu.pku.modeler.internal.IProjectModelRoot#initModel()" + NL + "\t */" + NL + "\tpublic void initModel(String modelName) {" + NL + "\t\t";
  protected final String TEXT_7 = " factory = new ";
  protected final String TEXT_8 = "();" + NL + "\t\t";
  protected final String TEXT_9 = " pkg = new ";
  protected final String TEXT_10 = "();" + NL + "\t\t// create model" + NL + "\t\tmodel = (MetaModel)factory.createElement(";
  protected final String TEXT_11 = ".__METAMODEL__, false);" + NL + "\t\t// register the model to ModelRegistry" + NL + "\t\tModelRegistry.getRegistry().registerModel(model, factory, pkg);" + NL + "\t\t// register the model to package" + NL + "\t\tfactory.registerToPackage(model, ";
  protected final String TEXT_12 = ".__METAMODEL__);" + NL + "\t\tMetaPackage defaultPackage = factory.createMetaPackage();" + NL + "\t\tdefaultPackage.setName(\"default_package\");" + NL + "\t\tdefaultPackage.setModel(model);" + NL + "\t\tmodel.setName(modelName);" + NL + "\t\tmodel.getPackages().add(defaultPackage);" + NL + "\t\t// by default, the target modeling tool need not these primitive types" + NL + "//\t\tinitPrimitiveType(factory);" + NL + "\t}" + NL + "\t/*" + NL + "\tprotected void initPrimitiveType(CoreFactory factory) {" + NL + "\t\tMetaPrimitiveType booleanType = factory.createMetaPrimitiveType();" + NL + "\t\tMetaPrimitiveType integerType = factory.createMetaPrimitiveType();" + NL + "\t\tMetaPrimitiveType floatType = factory.createMetaPrimitiveType();" + NL + "\t\tMetaPrimitiveType doubleType = factory.createMetaPrimitiveType();" + NL + "\t\tMetaPrimitiveType charType = factory.createMetaPrimitiveType();" + NL + "\t\tMetaPrimitiveType stringType = factory.createMetaPrimitiveType();" + NL + "\t\tbooleanType.setName(\"boolean\");" + NL + "\t\tintegerType.setName(\"int\");" + NL + "\t\tfloatType.setName(\"float\");" + NL + "\t\tdoubleType.setName(\"double\");" + NL + "\t\tcharType.setName(\"char\");" + NL + "\t\tstringType.setName(\"String\");" + NL + "\t\t" + NL + "\t\tbooleanType.setModel(model);" + NL + "\t\tintegerType.setModel(model);" + NL + "\t\tfloatType.setModel(model);" + NL + "\t\tdoubleType.setModel(model);" + NL + "\t\tcharType.setModel(model);" + NL + "\t\tstringType.setModel(model);" + NL + "\t\t" + NL + "\t\tList packages = model.getPackages();" + NL + "\t\tif(packages.size() > 0) {" + NL + "\t\t\tMetaPackage defaultPackage = (MetaPackage)packages.get(0);" + NL + "\t\t\tdefaultPackage.getNamedElements().add(booleanType);" + NL + "\t\t\tdefaultPackage.getNamedElements().add(integerType);" + NL + "\t\t\tdefaultPackage.getNamedElements().add(floatType);" + NL + "\t\t\tdefaultPackage.getNamedElements().add(doubleType);" + NL + "\t\t\tdefaultPackage.getNamedElements().add(charType);" + NL + "\t\t\tdefaultPackage.getNamedElements().add(stringType);" + NL + "\t\t}" + NL + "\t\tmodel.registerType(booleanType);" + NL + "\t\tmodel.registerType(integerType);" + NL + "\t\tmodel.registerType(floatType);" + NL + "\t\tmodel.registerType(doubleType);" + NL + "\t\tmodel.registerType(charType);" + NL + "\t\tmodel.registerType(stringType);" + NL + "\t}" + NL + "\t*/" + NL + "\t/**" + NL + "\t * @return Returns the modelName." + NL + "\t */" + NL + "\tpublic String getModelName() {" + NL + "\t\treturn model.getName();" + NL + "\t}" + NL + "\t/**" + NL + "\t * @param modelName The modelName to set." + NL + "\t */" + NL + "\tpublic void setModelName(String modelName) {" + NL + "\t\tmodel.setName(modelName);" + NL + "\t}" + NL + "\t" + NL + "\tpublic void addToModel(Object modelEle, MetaDiagram diagram) {" + NL + "//\t\tMetaModelContentProvider provider = (MetaModelContentProvider)treeViewer.getContentProvider();" + NL + "//\t\tObject parent = provider.getParent(diagram);" + NL + "//\t\tif(parent instanceof MetaPackage) {" + NL + "//\t\t\tMetaPackage p = (MetaPackage)parent;" + NL + "//\t\t\tif(modelEle instanceof MetaNamedElement) {" + NL + "//\t\t\t\tp.getNamedElements().add(modelEle);" + NL + "//\t\t\t}" + NL + "//\t\t\telse {" + NL + "//\t\t\t\tp.getOtherElements().add(modelEle);" + NL + "//\t\t\t}" + NL + "//\t\t\tmodel.registerType(modelEle);" + NL + "//\t\t}" + NL + "//\t\tif(modelEle instanceof MetaModelElement) {" + NL + "//\t\t\t((MetaModelElement)modelEle).setModel(model);" + NL + "//\t\t\t((MetaModelElement)modelEle).addPropertyChangeListener(provider.getModelListener());" + NL + "//\t\t}" + NL + "//\t\ttreeViewer.refresh(parent);" + NL + "\t}" + NL + "\t" + NL + "\t" + NL + "\t//????Editor?????????????treeViewer??????" + NL + "\tpublic void removeFromModel(Object modelEle, MetaDiagram diagram) {" + NL + "//\t\tMetaModelContentProvider provider = (MetaModelContentProvider)treeViewer.getContentProvider();" + NL + "//\t\tObject parent = provider.getParent(diagram);" + NL + "//\t\tif(parent instanceof MetaPackage) {" + NL + "//\t\t\tMetaPackage p = (MetaPackage)parent;" + NL + "//\t\t\tif(modelEle instanceof MetaNamedElement) {" + NL + "//\t\t\t\tp.getNamedElements().remove(modelEle);" + NL + "//\t\t\t}" + NL + "//\t\t\telse {" + NL + "//\t\t\t\tp.getOtherElements().remove(modelEle);" + NL + "//\t\t\t}" + NL + "//\t\t}" + NL + "//\t\ttreeViewer.refresh(parent);" + NL + "\t}" + NL + "//\t/**" + NL + "//\t * @return Returns the treeViewer." + NL + "//\t */" + NL + "//\tpublic TreeViewer getTreeViewer() {" + NL + "//\t\treturn treeViewer;" + NL + "//\t}" + NL + "//\t/**" + NL + "//\t * @param treeViewer The treeViewer to set." + NL + "//\t */" + NL + "//\tpublic void setTreeViewer(TreeViewer treeViewer) {" + NL + "//\t\tthis.treeViewer = treeViewer;" + NL + "//\t}" + NL + "\t" + NL + "\tpublic boolean hasDiagram(Object diagram) {" + NL + "\t\tif(model == null)return false;" + NL + "\t\tList<MetaPackage> pkgs = model.getPackages();" + NL + "\t\tfor(MetaPackage pkg:pkgs) {" + NL + "\t\t\tif(hasDiagram(pkg, diagram))return true;" + NL + "\t\t}" + NL + "\t\treturn false;" + NL + "\t}" + NL + "\t" + NL + "\tprivate boolean hasDiagram(MetaPackage parent, Object diagram) {" + NL + "\t\tList<MetaDiagram> diags = parent.getDiagrams();" + NL + "\t\tfor(MetaDiagram diag:diags) {" + NL + "\t\t\tif(diag==diagram)return true;" + NL + "\t\t}" + NL + "\t\tList<MetaPackage> pkgs = parent.getSubPackages();" + NL + "\t\tfor(MetaPackage pkg:pkgs) {" + NL + "\t\t\tif(hasDiagram(pkg, diagram))return true;" + NL + "\t\t}" + NL + "\t\treturn false;" + NL + "\t}" + NL + "\t" + NL + "\t/**" + NL + "\t * @return Returns the model." + NL + "\t */" + NL + "\tpublic MetaModel getModel() {" + NL + "\t\treturn model;" + NL + "\t}" + NL + "\t/**" + NL + "\t * @param model The model to set." + NL + "\t */" + NL + "\tpublic void setModel(MetaModel model) {" + NL + "\t\tthis.model = model;" + NL + "\t}" + NL + "}";
  protected final String TEXT_13 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.internalBase);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.modelBase + "." + ModelerConstants.modelFactory);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(ModelerConstants.pkgBase + "." + ModelerConstants.modelBase + "." + ModelerConstants.modelPackage);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(ModelerConstants.modelRootName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(ModelerConstants.modelRootName);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(ModelerConstants.modelFactory);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(ModelerConstants.modelFactory);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(ModelerConstants.modelPackage);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(ModelerConstants.modelPackage);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(ModelerConstants.modelPackage);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(ModelerConstants.modelPackage);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(TEXT_13);
    return stringBuffer.toString();
  }
}
